package com.zmn.oms.zmn.normal.business.impl.order.advancequote;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.base.common.data.common.dro.phenomenon.PhenomenonDRO;
import com.zmn.base.plat.engine.common.constant.DubboConsts;
import com.zmn.base.price.common.dto.fixed.FixedPhenoFilterItemQuery;
import com.zmn.base.price.common.dto.item.ItemCellDRO;
import com.zmn.base.price.common.dto.item.ItemDRO;
import com.zmn.base.price.common.dto.item.ItemQuoteQuery;
import com.zmn.base.price.dubbo.interfaces.calculate.FixedFlowRemoteService;
import com.zmn.base.price.dubbo.interfaces.calculate.ItemPriceRemoteService;
import com.zmn.base.product.common.consts.BaseProductConsts;
import com.zmn.base.product.common.dto.product.ProductBaseQuery;
import com.zmn.base.product.common.dto.product.ec.ProductBaseDRO;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductForeignListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.amount.AmountUtil;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.interfaces.orderfinalpricequotation.OrderFinalPriceQuotationBService;
import com.zmn.oms.business.interfaces.product.OrderProductExtendBService;
import com.zmn.oms.common.annotation.ZsNormalOrderWorkLog;
import com.zmn.oms.common.constant.OrderAdvanceConsts;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.enums.ServiceItemTypeEnum;
import com.zmn.oms.model.entity.factory.OrderFactory;
import com.zmn.oms.model.entity.orderfinalpricequotation.OrderFinalPriceQuotationQuery;
import com.zmn.oms.services.interfaces.orderfinalpricequotation.OrderFinalPriceQuotationService;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import com.zmn.oms.services.interfaces.serviceitem.OrderServiceItemService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.normal.business.interfaces.order.advancequote.AdvanceQuoteModifyBService;
import com.zmn.oms.common.constant.OmsErrorCodeConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.order.OrderDTO;
import com.zmn.oms.model.dto.serviceitem.AddOrderServiceItemDTO;
import com.zmn.oms.model.entity.orderfinalpricequotation.OrderFinalPriceQuotation;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.model.entity.product.OrderProductExtend;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItemCell;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.zmn.normal.business.interfaces.serviceitem.ZsNormalOrderServiceItemBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sunlife
 * @date:2022/9/25 下午6:05
 * description:
 */
@Service
@Slf4j
public class AdvanceQuoteModifyBServiceImpl implements AdvanceQuoteModifyBService {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ItemPriceRemoteService itemPriceRemoteService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private FixedFlowRemoteService fixedFlowRemoteService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private ProductForeignListRemoteService productForeignListRemoteService;

    @Resource
    private ZsNormalOrderServiceItemBService zsNormalOrderServiceItemBService;
    @Resource
    private OrderServiceItemService orderServiceItemService;
    @Resource
    private OrderProductExtendBService orderProductExtendBService;
    @Resource
    private OrderProductService orderProductService;
    @Autowired
    protected OrderFinalPriceQuotationService orderFinalPriceQuotationService;
    @Autowired
    protected OrderWorkService orderWorkService;
    @Autowired
    protected OrderFinalPriceQuotationBService orderFinalPriceQuotationBService;

    @Resource
    protected AdvanceQuoteModifyBService advanceQuoteModifyBService;

    @Override
    @Transactional
    public void processUpdateAdvanceQuote(OrderDTO orderDTO) throws OmsBaseException {
        if(Objects.isNull(orderDTO.getOrderFinalPriceQuotation())) {
            return;
        }

        OrderWork dbOrderWork = orderDTO.getDbOrderWork();
        OrderWork orderWork = orderDTO.getOrderWork();
        OrderFactory dbOrderFactory = orderDTO.getDbOrderFactory();
        OrderFactory orderFactory = orderDTO.getOrderFactory();
        List<OrderProduct> dbOrderProductList = orderDTO.getDbOrderProductList();
        List<OrderProduct> orderProductList = orderDTO.getOrderProductList();

        // 报价信息
        OrderFinalPriceQuotationQuery quotationQuery = new OrderFinalPriceQuotationQuery();
        quotationQuery.setOrderId(dbOrderWork.getOrderId());
        quotationQuery.setWorkId(dbOrderWork.getWorkId());
        OrderFinalPriceQuotation dbQuotation = null;
        List<OrderFinalPriceQuotation> dbOrderFinalPriceQuotationList = orderFinalPriceQuotationService.listByQuery(quotationQuery);
        if (CollectionUtil.isNotNullOrEmpty(dbOrderFinalPriceQuotationList)) {
            dbQuotation = dbOrderFinalPriceQuotationList.get(0);
        }

        // 如果之前没有操作过提前报价
        if (Objects.isNull(dbQuotation)) {
            advanceQuoteModifyBService.processUnAdvanceQuote(orderDTO);
            return;
        }

        // 更新逻辑

        // 产品ID是否变更
        boolean isProductIdChanged;
        // 产品数量变化
        boolean isProductNumberChanged;
        if (Objects.equals(dbOrderWork.getSourceType(), OrderConsts.ORDER_SOURCE_TYPE_FACTORY)) {
            isProductIdChanged = ( !Objects.equals(dbOrderFactory.getFcProductId(), orderFactory.getFcProductId()) )
                    || ( !Objects.equals(dbOrderWork.getServCategId(), orderWork.getServCategId())
                    || !Objects.equals(orderWork.getProductId(), dbOrderWork.getProductId()) );
            isProductNumberChanged = !Objects.equals(Optional.ofNullable(dbOrderFactory.getFcProductNumber()).orElse(1),
                    orderFactory.getFcProductNumber());
        } else {
            isProductIdChanged = !Objects.equals(dbOrderWork.getProductId(), orderWork.getProductId()) || !Objects.equals(dbOrderWork.getShowProductId(), orderWork.getShowProductId());
            isProductNumberChanged = !Objects.equals(dbOrderProductList.get(0).getNumber(), orderProductList.get(0).getNumber());
        }
        // 前台产品ID是否变化
        boolean isShowProductIdChanged = !Objects.equals(dbOrderWork.getShowProductId(), orderWork.getShowProductId());

        // 渠道变化
        boolean channelChanged = !NumberUtil.isNullOrZero(orderWork.getChannelId()) && !Objects.equals(orderWork.getChannelId(), dbOrderWork.getChannelId());
        // 城市变化
        boolean cityChanged = !NumberUtil.isNullOrZero(orderWork.getCityId()) && !Objects.equals(orderWork.getCityId(), dbOrderWork.getCityId());
        // 产品变化
        boolean isProductChanged = isProductIdChanged || isProductNumberChanged;
        // 品牌变化
        boolean isBrandChanged = !NumberUtil.isNullOrZero(orderProductList.get(0).getBrandId()) && !Objects.equals(orderProductList.get(0).getBrandId(), dbOrderProductList.get(0).getBrandId());


        // 判断故障现象、服务项是否有变更
        OrderFinalPriceQuotation quotation = orderDTO.getOrderFinalPriceQuotation();

        boolean isPhenIdChanged = !Objects.equals(dbQuotation.getPhenId(), quotation.getPhenId());

        // 查询数据库该订单最新的服务项
        List<OrderServiceItem> orderServiceItemList = orderServiceItemService.listByOrderIdSrcMaster(orderDTO.getOrderId());
        boolean isServItemIdChanged = false;
        if (CollectionUtil.isNotNullOrEmpty(orderServiceItemList)) {
            isServItemIdChanged = orderServiceItemList.stream()
                    .filter(item -> !Objects.equals(item.getServItemGroupId(), ServiceItemTypeEnum.ADDITION_ITEM.getType()))
                    .allMatch(item -> Objects.equals(item.getServItemId(), quotation.getServItemId()));
        } else {
            isServItemIdChanged = true;
        }

        if (!isShowProductIdChanged
                && !channelChanged
                && !cityChanged
                && !isProductChanged
                && !isPhenIdChanged
                && !isServItemIdChanged
                && !isBrandChanged) {
            log.info("订单[{}]相关信息无变更", dbOrderWork.getOrderId());
            return;
        }

        Integer itemId = quotation.getServItemId();
        if (NumberUtil.isNullOrZero(itemId)) {
            return;
        }

        // 如果产品、渠道、城市、品牌发生变更，重新生成服务项，确保价格正确
        advanceQuoteModifyBService.updateAdvanceQuote(orderDTO, dbQuotation);

    }

    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ADVANCE_QUOTE, beforeProceed = false, handleLog = false)
    public void processUnAdvanceQuote(OrderDTO orderDTO) throws OmsBaseException {


        // 生成服务项，并把服务项信息传递给orderDTO方便后边取值
        List<OrderServiceItem> serviceItemList = this.saveServiceItem(orderDTO);

        OrderWork orderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderDTO.getOrderId(), orderDTO.getWorkId());
        OrderProduct orderProduct = orderProductService.listOrderProductByOrderIdSrcMaster(orderDTO.getOrderId()).get(0);

        // 保存相关信息
        OrderFinalPriceQuotation quotation = this.getOrderFinalPriceQuotation(orderDTO, orderWork, orderProduct, serviceItemList);

        orderFinalPriceQuotationBService.saveOrderFinalPriceQuotation(orderDTO, quotation, orderWork, orderProduct);

        StringBuilder sb = new StringBuilder();
        if (StringUtil.isNotBlank(quotation.getPhenName())) {
            sb.append(" 故障现象:").append(quotation.getPhenName()).append("\n");
        } else {
            sb.append(" 服务项:").append(quotation.getServItemName()).append("\n");
        }
        sb.append(" 产品类别:").append(quotation.getCategName()).append("\n");
        sb.append(" 产品型号:").append(quotation.getTariffName()).append("\n");
        sb.append(" 服务价格:").append(AmountUtil.fen2Yuan(quotation.getTotalAmount())).append("元");
        orderDTO.setOperatorLogRemark(sb.toString());
    }

    @ZsNormalOrderWorkLog(type = OrderLogConsts.ORDER_LOG_TYPE_ADVANCE_QUOTE, beforeProceed = false, handleLog = false)
    public void updateAdvanceQuote(OrderDTO orderDTO, OrderFinalPriceQuotation dbQuotation) throws OmsBaseException {

        // 生成服务项，并把服务项信息传递给orderDTO方便后边取值
        List<OrderServiceItem> serviceItemList = this.saveServiceItem(orderDTO);

        OrderWork orderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderDTO.getOrderId(), orderDTO.getWorkId());
        OrderProduct orderProduct = orderProductService.listOrderProductByOrderIdSrcMaster(orderDTO.getOrderId()).get(0);

        // 保存相关信息
        OrderFinalPriceQuotation newQuotation = this.getOrderFinalPriceQuotation(orderDTO, orderWork, orderProduct, serviceItemList);

        newQuotation.setFinalPriceId(dbQuotation.getFinalPriceId());

        orderFinalPriceQuotationBService.updateOrderFinalPriceQuotation(orderDTO, newQuotation, orderWork, orderProduct);

        StringBuilder sb = new StringBuilder();
        sb.append("故障现象:");
        if (!Objects.equals(dbQuotation.getPhenName(), newQuotation.getPhenName())) {
            sb.append(StringUtils.defaultIfBlank(dbQuotation.getPhenName(), "空"));
            sb.append("->");
        }
        sb.append(StringUtils.defaultIfBlank(newQuotation.getPhenName(), "空")).append("\n");

        sb.append("服务项:");
        if (!Objects.equals(dbQuotation.getServItemName(), newQuotation.getServItemName())) {
            sb.append(StringUtils.defaultIfBlank(dbQuotation.getServItemName(), "空"));
            sb.append("->");
        }
        sb.append(StringUtils.defaultIfBlank(newQuotation.getServItemName(), "空")).append("\n");

        sb.append("产品类别:");
        if (!Objects.equals(dbQuotation.getCategName(), newQuotation.getCategName())) {
            sb.append(StringUtils.defaultIfBlank(dbQuotation.getCategName(), "空"));
            sb.append("->");
        }
        sb.append(StringUtils.defaultIfBlank(newQuotation.getCategName(), "空")).append("\n");


        sb.append("产品型号:");
        if (!Objects.equals(dbQuotation.getTariffName(), newQuotation.getTariffName())) {
            sb.append(StringUtils.defaultIfBlank(dbQuotation.getTariffName(), "空"));
            sb.append("->");
        }
        sb.append(StringUtils.defaultIfBlank(newQuotation.getTariffName(), "空")).append("\n");

        sb.append(" 服务价格:");
        if (!Objects.equals(dbQuotation.getTotalAmount(), newQuotation.getTotalAmount())) {
            if (NumberUtil.isNotNullOrZero(dbQuotation.getTotalAmount())) {
                sb.append(AmountUtil.fen2Yuan(dbQuotation.getTotalAmount())).append("元");
            }
            sb.append("->");
        }
        sb.append(AmountUtil.fen2Yuan(Optional.ofNullable(newQuotation.getTotalAmount()).orElse(GlobalConsts.NONE))).append("元");
        orderDTO.setOperatorLogRemark(sb.toString());
    }

    /**
     * zmn 产品处理
     *
     * @param orderDTO
     * @return
     */
    protected OrderFinalPriceQuotation getOrderFinalPriceQuotation(OrderDTO orderDTO, OrderWork orderWork, OrderProduct orderProduct, List<OrderServiceItem> serviceItemList) {
        OrderFinalPriceQuotation quotation = BeanMapper.map(orderDTO.getOrderFinalPriceQuotation(), OrderFinalPriceQuotation.class);

        quotation.setOrderId(orderWork.getOrderId());
        quotation.setWorkId(orderWork.getWorkId());

        Integer quoteType = quotation.getQuotationType();
        if (NumberUtil.isNullOrZero(quoteType)) {
            // 冗余处理，如果没有传，判断故障现象ID
            if (NumberUtil.isNotNullOrZero(quotation.getPhenId())) {
                quoteType = OrderAdvanceConsts.QUOTE_TYPE_FAULT_PHEN;
            } else if (NumberUtil.isNotNullOrZero(quotation.getServItemId())) {
                quoteType = OrderAdvanceConsts.QUOTE_TYPE_SERVICE_ITEM_TYPE;
            } else {
                quoteType = com.zmn.consts.GlobalConsts.NONE;
            }
        }

        quotation.setQuotationType(quoteType);

        quotation.setCategId(orderProduct.getCategId());
        quotation.setCategName(orderProduct.getCategName());
        quotation.setProductId(orderProduct.getProductId());
        quotation.setProductName(orderProduct.getProductName());
        quotation.setShowProductId(orderProduct.getShowProductId());
        quotation.setShowProductName(orderProduct.getShowProductName());
        quotation.setBrandId(orderProduct.getBrandId());
        quotation.setBrandName(orderProduct.getBrandName());

        ProductBaseQuery productBaseQuery = ProductBaseQuery.builder()
                .showType(BaseProductConsts.ERP_SHOW_TYPE)
                .productId(orderProduct.getProductId())
                .build();
        ResponseDTO<List<ProductBaseDRO>> productResponseDTO = productForeignListRemoteService.listBaseDROByQuery(productBaseQuery);
        if (productResponseDTO.isSuccess() && CollectionUtil.isNotNullOrEmpty(productResponseDTO.getData())) {
            ProductBaseDRO productBaseDRO = productResponseDTO.getData().get(0);
            quotation.setTariffName(productBaseDRO.getTariffName());
            quotation.setOldTariffName(productBaseDRO.getTariffName());
        }

        // 服务项相关信息
        if (CollectionUtil.isNotNullOrEmpty(serviceItemList)) {
            OrderServiceItem serviceItem = serviceItemList.get(0);
            quotation.setServItemId(serviceItem.getServItemId());
            quotation.setServItemName(serviceItem.getServItemName());
            quotation.setItemPrice(serviceItem.getItemPrice());
            quotation.setItemUnit(serviceItem.getItemUnit());
        }

        quotation.setTotalAmount(Optional.ofNullable(orderWork.getTotalAmount()).orElse(0));

        quotation.setCreater(orderDTO.getOperator());
        quotation.setUpdater(orderDTO.getOperator());

        Date now = DateUtil.getNow();
        quotation.setUpdateTime(now);
        quotation.setCreateTime(now);

        // 冗余处理故障现象
        if (NumberUtil.isNullOrZero(quotation.getPhenId())) {
            // 没传，置空，后边查到冗余，查不到，删除数据库数据
            quotation.setPhenId(GlobalConsts.NONE);
            quotation.setPhenName("".intern());
            quotation.setDeteTime(GlobalConsts.NONE);
        }

        if (NumberUtil.isNullOrZero(quotation.getPhenId())
                && NumberUtil.isNotNullOrZero(quotation.getServItemId())
                && Objects.equals(orderWork.getServItemType(), OrderConsts.SERVICE_ITEM_TYPE_QUOTATION_THREE)
                && Objects.equals(orderWork.getFinalPrice(), GlobalConsts.YES)
        ) {
            // 通过服务项ID，完善故障现象信息
            FixedPhenoFilterItemQuery itemQuery = FixedPhenoFilterItemQuery.builder()
                    .bizType(orderWork.getBizType())
                    .servCategId(orderWork.getServCategId())
                    .ecProductId(orderProduct.getShowProductId())
                    .itemId(quotation.getServItemId())
                    .brandId(orderProduct.getBrandId())
                    .categId(orderProduct.getCategId())
                    .channelId(orderWork.getChannelId())
                    .cityId(orderWork.getCityId())
                    .build();

            log.info("通过服务项ID查询故障现象 入参：{}", JSON.toJSONString(itemQuery));
            com.zmn.common.dto2.ResponseDTO<List<PhenomenonDRO>> responseDTO = fixedFlowRemoteService.listPhenoFilterItemByQuery(itemQuery);
            log.info("通过服务项ID查询故障现象 出参：{}", JSON.toJSONString(responseDTO));
            if (responseDTO.isSuccess() && com.zmn.common.utils.collection.CollectionUtil.isNotNullOrEmpty(responseDTO.getData())) {
                PhenomenonDRO phenomenonDRO = responseDTO.getData().get(0);
                quotation.setPhenId(phenomenonDRO.getPhenId());
                quotation.setPhenName(phenomenonDRO.getPhenName());
                quotation.setDeteTime(phenomenonDRO.getMaintainTime());
            }
        }

        return quotation;
    }

    private List<OrderServiceItem> saveServiceItem(OrderDTO orderDTO) throws OmsBaseException {
        Integer itemId = orderDTO.getOrderFinalPriceQuotation().getServItemId();
        if (NumberUtil.isNullOrZero(itemId)) {
            return Lists.newArrayList();
        }
        
        OrderWork orderWork = orderDTO.getOrderWork();
        OrderWork dbOrderWork = orderDTO.getDbOrderWork();

        OrderProduct orderProduct = orderDTO.getOrderProductList().get(0);
        OrderProduct dbOrderProduct = orderDTO.getDbOrderProductList().get(0);

        // 保存前端选中的服务项
        List<OrderProductExtend> extendList = orderProductExtendBService.getOrderProductExtend(orderWork.getOrderId());

        Long proExtId = extendList.get(0).getProExtId();

        AddOrderServiceItemDTO addOrderServiceItemDTO = new AddOrderServiceItemDTO();
        addOrderServiceItemDTO.setOrderId(Optional.ofNullable(orderWork.getOrderId()).orElse(dbOrderWork.getOrderId()));
        addOrderServiceItemDTO.setWorkId(Optional.ofNullable(orderWork.getWorkId()).orElse(dbOrderWork.getWorkId()));
        addOrderServiceItemDTO.setProExtId(proExtId);
        addOrderServiceItemDTO.setQuotationType(com.zmn.consts.GlobalConsts.OPERATE_USER_TYPE_STAFF);
        addOrderServiceItemDTO.setIsProductChanged(false);

        // 处理服务项
        ItemQuoteQuery query = ItemQuoteQuery
                .builder()
                .bizType(Optional.ofNullable(orderWork.getBizType()).orElse(dbOrderWork.getBizType()))
                .channelId(Optional.ofNullable(orderWork.getChannelId()).orElse(dbOrderWork.getChannelId()))
                .cityId(Optional.ofNullable(orderWork.getCityId()).orElse(dbOrderWork.getCityId()))
                .productId(Optional.ofNullable(orderProduct.getProductId()).orElse(dbOrderProduct.getProductId()))
                .brandId(Optional.ofNullable(orderProduct.getBrandId()).orElse(dbOrderProduct.getBrandId()))
                .tariffId(Optional.ofNullable(orderProduct.getTariffId()).orElse(dbOrderProduct.getTariffId()))
                .itemList(Lists.newArrayList(itemId)).build();
        com.zmn.common.dto2.ResponseDTO<List<ItemDRO>> itemResponseDTO = itemPriceRemoteService.listItemQuoteByQuery(query);
        log.info("修改工单获取服务项：入参：{} 出参：{}", JSON.toJSONString(query), JSON.toJSONString(itemResponseDTO));
        if (!itemResponseDTO.isSuccess() || CollectionUtil.isNullOrEmpty(itemResponseDTO.getData())) {
            log.info("订单[{}]修改服务项失败，未查询到服务项", Optional.ofNullable(orderWork.getOrderId()).orElse(dbOrderWork.getOrderId()));
        }
        List<ItemDRO> itemDRO3List = itemResponseDTO.getData();
        ItemDRO item = itemDRO3List.stream().max(Comparator.comparing(ItemDRO::getPrice)).get();

        OrderServiceItem serviceItem = new OrderServiceItem();
        serviceItem.setServItemId(item.getItemId());
        serviceItem.setServItemName(item.getItemName());
        serviceItem.setInternalSettlementPrice(item.getPriceInternalSettlement());
        serviceItem.setInternalTotalPrice(item.getPriceInternalSettlement());
        serviceItem.setExternalSettlementPrice(item.getPriceExternalSettlement());
        serviceItem.setExternalTotalPrice(item.getPriceExternalSettlement());
        serviceItem.setItemPrice(item.getPrice());
        serviceItem.setTotalPrice(item.getPrice());
        serviceItem.setProductId(Optional.ofNullable(orderProduct.getProductId()).orElse(dbOrderProduct.getProductId()));
        serviceItem.setProductName(Optional.ofNullable(orderProduct.getProductName()).orElse(dbOrderProduct.getProductName()));
        serviceItem.setOrderId(Optional.ofNullable(orderWork.getOrderId()).orElse(dbOrderWork.getOrderId()));
        serviceItem.setNumber(1);
        serviceItem.setProExtId(proExtId);
        serviceItem.setServItemGroupId(item.getItemTypeId());
        serviceItem.setServItemGroupName(Optional.ofNullable(item.getItemTypeName()).orElse("默认值"));
        serviceItem.setServItemCategId(item.getItemTypeId());
        serviceItem.setTieredTotalPrice(item.getPrice());
        serviceItem.setOrigItemPrice(item.getPrice());
        serviceItem.setOrigTotalPrice(item.getPrice());
        serviceItem.setCreater(orderDTO.getOperator());
        serviceItem.setCreateTime(DateUtil.getNow());

        serviceItem.setItemUnit(item.getItemUnit());

        if (CollectionUtil.isNotNullOrEmpty(item.getItemCellList())) {
            List<OrderServiceItemCell> itemCellList = new ArrayList<>(item.getItemCellList().size());
            for (ItemCellDRO itemCell : item.getItemCellList()) {
                OrderServiceItemCell cell = new OrderServiceItemCell();
                cell.setItemCellId(itemCell.getItemCellId());
                cell.setItemCellName(itemCell.getItemCellName());
                cell.setCellPrice(itemCell.getCellPrice());
                cell.setSort(itemCell.getSort());
                cell.setCreater(Optional.ofNullable(orderWork.getUpdater()).orElse("系统"));
                cell.setCreateTime(DateUtil.getNow());
                itemCellList.add(cell);
            }
            serviceItem.setItemDetailList(itemCellList);
        }
        addOrderServiceItemDTO.setOrderServiceItemList(Lists.newArrayList(serviceItem));
        addOrderServiceItemDTO.setVerifyPermission(false);
        zsNormalOrderServiceItemBService.saveServiceitem(addOrderServiceItemDTO);

        return Lists.newArrayList(serviceItem);
    }
}
